<?php
namespace app\admin\controller;
use \think\Db;
use think\Cache;
use think\Loader;
use think\Session;
use think\Controller;
//个人会员处理
/**
 * Class Member 个人会员管理控制器
 * @package app\admin\controller
 */
class Commodity extends Controller
{       
    //个人会员列表
    public function index()
    {
    	$param = $this->request->param();
    	$where = [];
        //条件查询
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.addtime'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.addtime'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.addtime'] = ['<= time', $endTime];
            }
        }
        $userName   = empty($param['name']) ? '' : $param['name'];
        if (!empty($userName)) {
            $where['a.name'] = $userName;
        }
        $type   = empty($param['type'])? '' : $param['type'];
        if (!empty($type)) {
            $where['a.type'] = $type;
        }    
    	$rs=db::name('shop_config')->alias('a')
        ->join('mj_shop b','a.shopId=b.shopId','left')
        ->field('a.*,b.shopName')
        ->where($where)
        ->order('addtime desc')
        ->paginate(10);
    	$count=db::name('shop_config')->where($where)->count();
    	$this->assign("count", $count);
        $this->assign("filter", $param);//设置过滤条件
    	$this->assign('arr',$rs);
        return $this->fetch();
    }

    //新增商店
    public function add(){
        $arr=db::name('shop')->field('shopId,shopName')->select();

        $this->assign('arr',$arr);
        return $this->fetch();
    }

    //新增商店提交
    public function addPost(){
       $param = $this->request->param();
       if($param['type']==2&&$param['shopId']==''){
            return json(['code'=>400,'msg'=>'门店商品必须选择商店']);
       }
       $arr=[
            'name'          =>$param['name'],
            'shopId'        =>$param['type']==2?$param['shopId']:'',
            'shopMoney'     =>$param['shopMoney'],
            'discountMoney' =>$param['discountMoney'],
            'type'          =>$param['type'],
            'addtime'       =>date('Y-m-d H:i:s')
       ];
       $rs=db::name('shop_config')->insert($arr);
       if($rs){
            return json(['code'=>200,'msg'=>'添加成功！']);
       }else{
            return json(['code'=>400,'msg'=>'添加失败！']);
       }

    }

    //修改商店
    public function edit($id){
        $arr=db::name('shop_config')->where('id',$id)->find();

        $rs=db::name('shop')->field('shopId,shopName')->select();

        if($arr['shopId']!=='0'){
          $vo=db::name('shop')->where('shopId',$arr['shopId'])->field('shopId,shopName')->find();
          $this->assign('vo',$vo);  
        }

        $this->assign('arr',$arr);
        $this->assign('rs',$rs);
        $this->assign('id',$id);
        return $this->fetch();
    }

    //新增商店提交
    public function editPost(){
       $param = $this->request->param();
       //dump($param);exit;
       if($param['type']==2&&$param['shopId']==''){
            return json(['code'=>400,'msg'=>'门店商品必须选择商店']);
       }
       $arr=[
            'name'          =>$param['name'],
            'shopId'        =>$param['type']==2?$param['shopId']:'',
            'shopMoney'     =>$param['shopMoney'],
            'discountMoney' =>$param['discountMoney'],
            'type'          =>$param['type'],
       ];
       $rs=db::name('shop_config')->where('id',$param['id'])->update($arr);
       if($rs){
            return json(['code'=>200,'msg'=>'修改成功！']);
       }else{
            return json(['code'=>400,'msg'=>'修改失败！']);
       }

    }

    //删除
    public function del($id)
    {
        $result = Db::name('shop_config')->where('id', $id)->delete();
        if($result){
            return json(['code'=>200,'msg'=>'删除成功！']);
        }else {
            return json(['code'=>400,'msg'=>'删除失败！']);
        }
    }


    //改变个人会员状态
    public function changeState()
    {
        $userId = input('id');
        $userStatus = input('status');
        $userStatus = $userStatus==1?'2':'1';
        $result = Db::name('shop_config')->where('id', $userId)->update(['status' => $userStatus]);
        if($result){
            return json(['code'=>1,'msg'=>'状态设置成功！']);
        }else {
            return json(['code'=>1,'msg'=>'状态设置失败！']);
        }
    }
}